<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/12
 * Time: 16:16
 * 课程
 */
namespace app\franchisee\controller;
use think\Db;
class Course extends Base
{
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
      //课程类型
      public function index(){
          extract(input());
          $res = Db::table('up_course_type')->where('course_type_status',1)->select();
          return json(['status' => 200, 'msg' => $res]);
      }
      //课程
      public function course(){
          extract(input());
          $page = input('param.page')?input('param.page'):1;
          $num = 10;
          $limit = ($page-1)*$num;
          $course_type_id = input('param.course_type_id');
          $res = Db::table('up_course')
              ->where('f_id',$course_type_id)
              ->where('course_status',1)
              ->field('course_id,course_img,course_name,course_teacher_name,
              course_zixun_num,course_price')
              ->limit($limit,$num)
              ->select();
          return json(['status' => 200, 'msg' => $res]);
      }
      //课程详情
    public function detailCourse(){
        extract(input());
        $course_id = input('param.course_id');
        $res = Db::table('up_course')->where('course_id',$course_id)->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    //观看视频
    public function video(){
        extract(input());
        $course_id = input('param.course_id');
        $res = Db::table('up_course_chapter')->where('course_id',$course_id)->select();
        $where['user_id'] =$user_id;
        $where['course_id'] =$course_id;
        $info = Db::table('up_course_like')->where($where)->find();
        if($info['course_like_id']!=''){
            $list['is_like'] = 1;
        }else{
            $list['is_like'] = 2;
        }

        $list['res'] = $res;
         return json(['status' => 200, 'msg' => $list]);

    }
    //收藏课程
    public function likeCourse(){
        extract(input());
        $course_id = input('param.course_id');
        $type = input('param.type');
        if($type==1){//收藏
            $where['user_id'] = $user_id;
            $where['course_id'] = $course_id;
            $info = Db::table('up_course_like')->where($where)->find();
            if($info['course_like_id']==''){
                $data['user_id'] = $user_id;
                $data['course_id'] = $course_id;
                $data['course_like_time'] = date('Y-m-d H:i:s');
                $res = Db::table('up_course_like')->insertGetId($data);
            }
        }elseif ($type==2){//取消收藏
           $where['user_id'] = $user_id;
           $where['course_id'] = $course_id;
            $res = Db::table('up_course_like')->where($where)->delete();
        }
        return json(['status' => 200, 'msg' => "操作成功"]);
    }
    //我的收藏
    public function myLike(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $res = Db::table('up_course_like')->alias('a')
            ->join('up_course uc','uc.course_id = a.course_id','left')
            ->where('a.user_id',$user_id)
            ->field('uc.*,a.course_like_id')
            ->limit($limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            $info = Db::table('up_course_chapter')->where('course_id',$vo['course_id'])->select();
            $vo['info'] = $info;
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    //题库
    public function question(){
        extract(input());
        $course_id = input('param.course_id');
        $where['course_id'] = $course_id;
        $res = Db::table('up_question')->where($where)->select();
        foreach ($res as $key=>&$vo){
            $info = Db::table('up_question_choice')->where('question_id',$vo['question_id'])->select();
            $vo['info'] = $info;
        }
        return json(['status' => 200, 'msg' => $res]);
    }
}